<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          left-icon="manager-o"
          v-model="adminAccount"
          name="adminAccount"
          label="账号"
          placeholder="请输入账号"
          :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
          v-model="adminPassword"
          type="password"
          name="adminPassword"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      >
        <template #left-icon>
          <svg-icon icon-name="icon-mima"></svg-icon>
        </template>
      </van-field>
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
    <van-cell title="用户登录" is-link to="/user/login"/>
    <van-cell title="没有账号？管理员注册" is-link to="/admin/register"/>
  </van-form>

</template>

<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import {ref} from "vue";
import myAxios from "../plugins/myAxios";
import {Toast} from "vant";
import SvgIcon from "../components/SvgIcon.vue";
const router = useRouter();
const route = useRoute();

const adminAccount = ref('');
const adminPassword = ref('');

const onSubmit = async () => {
  const res = await myAxios.post('/admin/login', {
    adminAccount: adminAccount.value,
    adminPassword: adminPassword.value,
  })
  console.log(res, '管理员登录');
  if (res.code === 0 && res.data) {
    Toast.success('登录成功');
    // 跳转到之前的页面
    const redirectUrl = route.query?.redirect as string ?? '/index';
    window.location.href = redirectUrl;
  } else {
    Toast.fail('登录失败');
  }
};

</script>

<style scoped>

</style>
